package com.zuipin.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.BaseProductBrand;
import com.zuipin.enums.SerialNumberEnum;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Jsons;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IBaseProductBrandService;
import com.zuipin.util.Pagination;
import com.zuipin.util.RedisUtils;
import com.zuipin.util.RegexUtils;
import com.zuipin.util.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商品品牌
 *
 * @ClassName BaseProductBrandController
 * @author wangcongyi
 * @date 2017年7月18日 上午11:52:29
 */
@Api(value = "商品品牌", description = "商品品牌-wangcongyi")
@Controller
@RequestMapping(value = "/baseProductBrand")
public class BaseProductBrandController {
	
	@Resource
	private IBaseProductBrandService	baseProductBrandService;
	@Resource
	private RedisUtils					redisUtils;
	
	@Jsons({ @Json(type = BaseProductBrand.class, include = "id,brandName"), })
	@RequestMapping(value = "/findProductBrand", method = RequestMethod.GET)
	@ApiOperation(value = "三期-获取商品品牌列表-wangcongyi", notes = "获取商品品牌列表" + "</br>返回码" + "</br>0-成功" + "</br>1004-系统异常")
	public Result<List<BaseProductBrand>> getOverallEval() {
		Result<List<BaseProductBrand>> result = new Result<>();
		result.setData(baseProductBrandService.selectProductBrand());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/selectBrandByNameOrKey", method = RequestMethod.GET)
	@ApiOperation(value = "查询品牌列表", notes = "查询品牌列表（模糊查询 名字和编号）-zhouhuihang")
	@ApiImplicitParams({ @ApiImplicitParam(name = "brandName", value = "品牌名称或编号", required = false, paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<BaseProductBrand>> selectBrandByNameOrKey(@RequestParam(required = false) String brandName, @Inject @ApiIgnore Pagination page) {
		PageResult<List<BaseProductBrand>> result = new PageResult<List<BaseProductBrand>>();
		result.setData(baseProductBrandService.selectProductBrandByName(brandName, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@Valids({ //
			@Valid(name = "isActive", required = false, maxValue = 1, minValue = 0), //
			@Valid(name = "createTime", required = false, regex = RegexUtils.DATE_FORMAT1), //
			@Valid(name = "sysId", required = true), //
			@Valid(name = "pageNumber", required = true, minValue = 1), //
			@Valid(name = "pageSize", required = true, minValue = 1), })
	@ResponseBody
	@RequestMapping(value = "/selectProductBrand", method = RequestMethod.GET)
	@ApiOperation(value = "后台查询品牌列表-xujinding", notes = "后台查询品牌列表-xujinding")
	@ApiImplicitParams({ //
			@ApiImplicitParam(name = "brandKey", value = "品牌编号", required = false, paramType = "query"), //
			@ApiImplicitParam(name = "brandName", value = "品牌名称", required = false, paramType = "query"), //
			@ApiImplicitParam(name = "isActive", value = "是否激活 0禁用 1激活或正常", required = false, paramType = "query"), //
			@ApiImplicitParam(name = "createTime", value = "创建时间  格式2017-05-06，注意不要时分秒", required = false, paramType = "query"), //
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = true, paramType = "query"), //
			@ApiImplicitParam(name = "pageSize", value = "页长", required = true, paramType = "query"), //
			@ApiImplicitParam(name = "sysId", value = "系统Id", required = true, paramType = "query"), //
	})
	public PageResult<List<BaseProductBrand>> selectProductBrand(@RequestParam(required = false) String brandName, //
			@RequestParam(required = false) String brandKey, //
			@RequestParam(required = false) Short isActive, //
			@RequestParam(required = false) String createTime, //
			@RequestParam(required = true) Integer pageNumber, //
			@RequestParam(required = true) Integer pageSize, //
			@RequestParam(required = true) Long sysId) {
		BaseProductBrand bpb = new BaseProductBrand();
		bpb.setBrandName(brandName);
		bpb.setBrandKey(brandKey);
		bpb.setIsActive(isActive);
		bpb.setCreateTime(createTime);
		bpb.setSysId(sysId);
		PageResult<List<BaseProductBrand>> pov = baseProductBrandService.selectProductBrand(bpb, pageNumber, pageSize);
		return pov;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insertProductBrand", method = RequestMethod.POST)
	@ApiOperation(value = "新增品牌", notes = "新增品牌-zhouhuihang")
	@Valids({ @Valid(name = "brandName", required = true, maxLength = 50, minLength = 0), //
			@Valid(name = "sysId", required = true), //
			@Valid(name = "logoUrl", required = false, maxLength = 250), //
			@Valid(name = "logoKey", required = false, maxLength = 250), //
			@Valid(name = "isActive", required = true, maxValue = 1, minValue = 0), //
			@Valid(name = "description", required = false, maxLength = 500), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "brandName", value = "品牌名称", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "logoUrl", value = "logo地址", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "logoKey", value = "logo键，删除使用", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "isActive", value = "是否激活 0禁用 1激活", required = true, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "description", value = "描述", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "createId", value = "创建者", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> insertProductBrand(@RequestParam String brandName, @RequestParam(required = false) String logoUrl, //
			@RequestParam(required = false) String logoKey, @RequestParam(required = false) String description, @RequestParam Long sysId, @RequestParam Short isActive, //
			@RequestParam(required = true) Long createId) throws ParseException {
		Result<Map<String, Object>> result = new Result<>();
		BaseProductBrand baseProductBrand = new BaseProductBrand();
		baseProductBrand.setSysId(sysId);
		baseProductBrand.setBrandName(brandName);
		baseProductBrand.setLogoUrl(logoUrl);
		baseProductBrand.setLogoKey(logoKey);
		baseProductBrand.setDescription(description);
		baseProductBrand.setBrandKey(redisUtils.getNumber(SerialNumberEnum.商品品牌编号));
		baseProductBrand.setIsActive(isActive);
		baseProductBrand.setCreateId(createId);
		baseProductBrand.setUpdateId(createId);
		baseProductBrandService.insert(baseProductBrand);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateProductBrand", method = RequestMethod.POST)
	@ApiOperation(value = "修改品牌", notes = "新增品牌-zhouhuihang")
	@Valids({ @Valid(name = "brandName", required = true, maxLength = 50, minLength = 0), //
			@Valid(name = "logoUrl", required = false, maxLength = 250), //
			@Valid(name = "logoKey", required = false, maxLength = 250), //
			@Valid(name = "description", required = false, maxLength = 500), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "brandId", value = "品牌Id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "brandName", value = "品牌名称", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "logoUrl", value = "logo地址", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "logoKey", value = "logo键，删除使用", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "description", value = "描述", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> updateProductBrand(@RequestParam Long brandId, @RequestParam(required = true) String brandName, //
			@RequestParam(required = false) String logoUrl, @RequestParam(required = false) String logoKey, @RequestParam(required = false) String description, //
			@RequestParam(required = true) Long updateId) {
		Result<Map<String, Object>> result = new Result<>();
		BaseProductBrand baseProductBrand = baseProductBrandService.findById(brandId);
		if (baseProductBrand == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("对象找不到");
		} else if (baseProductBrand.getIsActive() != null && BaseProductBrand.IS_ACTIVE_NO.equals(baseProductBrand.getIsActive())) {
			result.setErrorCode(ErrorCode.MODITY_BRAND_FAIL);
			result.setMessage("禁用的不允许修改");
		} else {
			baseProductBrand.setBrandName(brandName);
			if (StringUtil.isBlank(logoUrl)) {
				logoUrl = "";
			}
			if (StringUtil.isBlank(description)) {
				description = "";
			}
			
			if (StringUtil.isBlank(logoKey)) {
				logoKey = "";
			}
			baseProductBrand.setLogoKey(logoKey);
			baseProductBrand.setLogoUrl(logoUrl);
			baseProductBrand.setDescription(description);
			baseProductBrand.setUpdateId(updateId);
			baseProductBrandService.update(baseProductBrand);
			result.setErrorCode(ErrorCode.SUCCESS);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateProductBrandStatus", method = RequestMethod.POST)
	@ApiOperation(value = "品牌禁用与启动", notes = "品牌禁用与启动-zhouhuihang")
	@Valids({ @Valid(name = "brandId", required = true), //
			@Valid(name = "isActive", required = true, maxValue = 1, minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "brandId", value = "品牌Id", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "isActive", value = "是否激活 0禁用 1激活", required = true, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "更新者ID", required = true, dataType = "short", paramType = "query"), })
	public Result<Map<String, Object>> updateProductBrandStatus(@RequestParam Long brandId, @RequestParam Short isActive, @RequestParam(required = true) Long updateId) {
		Result<Map<String, Object>> result = new Result<>();
		BaseProductBrand baseProductBrand = baseProductBrandService.findById(brandId);
		if (baseProductBrand == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
		} else {
			baseProductBrand.setIsActive(isActive);
			baseProductBrand.setUpdateId(updateId);
			baseProductBrandService.update(baseProductBrand);
			result.setErrorCode(ErrorCode.SUCCESS);
		}
		return result;
	}
	
}
